<?php
session_start(); if (!isset($_SESSION['uid'])) header('Location: login.php');
require_once 'config.php';
function db(): PDO {
    static $pdo; if (!$pdo) {
        $dsn = "mysql:host=$GLOBALS[DB_HOST];dbname=$GLOBALS[DB_NAME];charset=$GLOBALS[DB_CHAR]";
        $pdo = new PDO($dsn, $GLOBALS['DB_USER'], $GLOBALS['DB_PASS'], [PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);
    } return $pdo;
}
$list = db()->query('SELECT g.id,g.name,g.code,g.unit,s.qty
                      FROM goods g
                      LEFT JOIN stock s ON g.id=s.goods_id
                      ORDER BY g.name')->fetchAll();
?>
<!doctype html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>库存 - 仓库管理系统 V 0.1</title>
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <link rel="stylesheet" href="bootstrap.min.css">
</head>
<body>
<nav class="navbar navbar-dark bg-primary">
  <div class="container-fluid">
    <span class="navbar-brand fw-bold">仓库管理系统 V 1.0</span>
    <a class="btn btn-outline-light btn-sm" href="index.php">返回首页</a>
  </div>
</nav>
<div class="container py-4">
  <h5 class="mb-3">实时库存</h5>
  <div class="table-responsive">
    <table class="table table-hover align-middle">
      <thead class="table-light">
        <tr><th>商品</th><th>编码</th><th>单位</th><th class="text-end">库存</th></tr>
      </thead>
      <tbody>
      <?php foreach ($list as $v): ?>
        <tr>
          <td><?= htmlspecialchars($v['name']) ?></td>
          <td><?= htmlspecialchars($v['code']) ?></td>
          <td><?= htmlspecialchars($v['unit']) ?></td>
          <td class="text-end fw-bold"><?= number_format($v['qty'] ?? 0) ?></td>
        </tr>
      <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>
<script src="bootstrap.bundle.min.js"></script>
</body>
</html>